//
//  BLRechargeOffLineViewController.m
//  blanket
//
//  Created by Mac on 2017/12/5.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "BLRechargeOffLineViewController.h"

@interface BLRechargeOffLineViewController ()
@property(nonatomic, strong) NSArray                     *dataSouce;
@property(nonatomic, strong) UITextField                     *moneyTextField;
@property(nonatomic, strong) UITextField                     *nameTextField;
@property(nonatomic, strong) UITextField                     *phoneTextField;
@end

@implementation BLRechargeOffLineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"线下充值";
    [self configSubView];
}

#pragma mark --subView
- (void)configSubView{
    
    STTableEasyModel * model1 = [[STTableEasyModel alloc] initWithTextString:@"充值方式" detailString:@"线下转款"];
    model1.accessoryType = UITableViewCellAccessoryNone;
    
    self.moneyTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth * 0.66, 44)];
    self.moneyTextField.keyboardType = UIKeyboardTypeDecimalPad;
    self.moneyTextField.placeholder = @"请输入充值额度";
    
    self.nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth * 0.66, 44)];
    self.nameTextField.placeholder = @"请输入转账人姓名";
    
    self.phoneTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth * 0.66, 44)];
    self.phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
    self.phoneTextField.placeholder = @"请输入联系方式";
    
    self.dataSouce = @[model1,
                       [[STTableEasyModel alloc] initWithTextString:@"充值额度" accessoryView:self.moneyTextField],
                       [[STTableEasyModel alloc] initWithTextString:@"转账人姓名" accessoryView:self.nameTextField],
                       [[STTableEasyModel alloc] initWithTextString:@"联系方式" accessoryView:self.phoneTextField]];
    [self.tableView reloadData];
    
    __weak typeof(self) weakSelf =  self;
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    footer.backgroundColor = self.tableView.backgroundColor;
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(30, 0, UIScreenWidth - 60, 44)
                                                        title:@"确定"
                                                   titleColor:[UIColor whiteColor]
                                                    titleFont:18
                                                 cornerRadius:5
                                              backgroundColor:BL_BlueBackGroundColor
                                              backgroundImage:nil
                                                        image:nil];
    [confimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedCommitButton];
    }];
    confimButton.st_centerY = 50;
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [footer addSubview:confimButton];
    self.tableView.tableFooterView = footer;
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count ;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44   ;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 14;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
        
    }
    cell.textLabel.textColor = UIColorFromRGBA(0x333333);
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
    cell.detailTextLabel.textColor = UIColorFromRGBA(0x999999);
    
    STTableEasyModel * mdoel = self.dataSouce[indexPath.row];
    cell.textLabel.text = mdoel.textString;
    cell.detailTextLabel.text = mdoel.detailString;
    if (mdoel.accessoryView) {
        cell.accessoryView = mdoel.accessoryView;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method
- (void)onSelectedCommitButton{

    if (!self.moneyTextField.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入金额"];
        return;
    }
    if (!self.nameTextField.text.length) {
         [SVProgressHUD showErrorWithStatus:@"请输入姓名"];
        return;
    }
    if (!self.phoneTextField.text.length) {
         [SVProgressHUD showErrorWithStatus:@"请输入联系方式"];
        return;
    }
    [self sendRechargeReuest];
}
- (void)sendRechargeReuest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:debugToken forKey:@"token"];
    [dic setObject:@"线下充值" forKey:@"method"];
    
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php/Api/Basics/lineRechargeAction"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if (self.moneyTextField.text.length) {
        [dic setObject:self.moneyTextField.text forKey:@"price"];
    }
    if (self.nameTextField.text.length) {
        [dic setObject:self.nameTextField.text forKey:@"name"];
    }
    if (self.phoneTextField.text.length) {
        [dic setObject:self.phoneTextField.text forKey:@"mobile"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 NSDictionary * dataDic =  responseObject[@"data"];
                                                 //NSLog(@"json = %@",dataDic.mj_JSONString);
                                                 [SVProgressHUD showSuccessWithStatus:@"已经收到您的申请"];
                                                 [self.navigationController popViewControllerAnimated:YES];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
@end
